package com.sti.framework.core.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sti.framework.core.annotation.authorization.Authorization;
import com.sti.framework.core.annotation.authorization.Authorizations;
import com.sti.framework.core.data.IDEntity;
import com.sti.framework.core.service.BaseService;
import com.sti.framework.core.web.Page;
import com.sti.framework.core.web.ResponseBodyEntity;
import com.sti.framework.core.web.SimplePage;
import com.sti.framework.models.Excel.lib.ExcelUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;

public class BaseController<M extends BaseMapper<T>, T, S>{

    @Inject
    private BaseService<M, T> baseService;

    Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[1];

    /**
     * 列表
     * @param page  分页内容
     * @param searchWhere   搜索内容
     * @return
     */
    @ApiOperation(value = "列表", notes = "列表")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorizations({
            @Authorization(tag = "/List", describes = "列表"),
    })
    @GetMapping("/List")
    public ResponseBodyEntity<SimplePage<T>> List(Page page, S searchWhere) {
        return ResponseBodyEntity.success(baseService.getListPage(page, searchWhere));
    };


    /**
     * 导出表格
     * @param searchWhere   搜索内容
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorizations({
            @Authorization(tag = "/Export", describes = "导出表格"),
    })
    @GetMapping("/Export")
    public void List(S searchWhere, HttpServletResponse response) {

        List<T> list = baseService.getList(searchWhere);

        try {
            ExcelUtils.exportExcel(list,null,"sheet1", tClass,"数据表格", response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    };

    /**
     * 增加
     */
    @ApiOperation(value = "增加", notes = "增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true),
    })
    @Authorization(tag = "/Create", describes = "增加", checkValid = false)
    @PostMapping(value = "/Create")
    public ResponseBodyEntity<T> Create(@RequestBody @Valid T data) {
        baseService.create(data);
        return ResponseBodyEntity.success(data);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fakeid", value = "被修改数据的fakeid", paramType = "header", dataTypeClass = String.class, required = true),
    })
    @Authorization(tag = "/Remove", describes = "删除")
    @PostMapping(value = "/Remove")
    public ResponseBodyEntity<String> Remove(@RequestBody @Valid IDEntity id) {
        baseService.delete(id.getId());
        return ResponseBodyEntity.success();
    };

    /**
     * 修改-全部属性
     */
    @ApiOperation(value = "修改-全部属性", notes = "修改-全部属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fakeid", value = "被修改数据的fakeid", paramType = "header", dataTypeClass = String.class, required = true),
    })
    @Authorization(tag = "/Modify", describes = "修改")
    @PostMapping(value = "/Modify")
    public ResponseBodyEntity<String> Modify(@RequestBody @Valid T data, @Valid IDEntity id) {
        baseService.update(data, id.getId());
        return ResponseBodyEntity.success();
    };

    /**
     * 修改-非空属性
     */
    @ApiOperation(value = "修改-非空属性", notes = "修改-非空属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fakeid", value = "被修改数据的fakeid", paramType = "header", dataTypeClass = String.class, required = true),
    })
    @Authorization(tag = "/Modify", describes = "修改", impor = "false")
    @PostMapping(value = "/ModifyNotNull")
    public ResponseBodyEntity<String> ModifyNotNull(@RequestBody T data, @Valid IDEntity id) {
        baseService.updateNotNull(data, id.getId());
        return ResponseBodyEntity.success();
    };

    /**
     * 详情
     */
    @ApiOperation(value = "详情", notes = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fakeid", value = "被修改数据的fakeid", paramType = "header", dataTypeClass = String.class, required = true),
    })
    @Authorization(tag = "/List", describes = "列表", impor = "false")
    @GetMapping("/Info")
    public ResponseBodyEntity<T> Info(@Valid IDEntity id) {
        T data = baseService.get(id.getId());
        // 数据不存在
        if (data == null){
            return ResponseBodyEntity.fail(ResponseBodyEntity.ResponseStatus.USER_ERROR_22101);
        }
        return ResponseBodyEntity.success(data);
    };
}
